import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:scanQrCode/page/qrcode_page.dart';
import 'package:scanQrCode/page/scancode_page.dart';
import 'package:scanQrCode/utils/Toast.dart';
import 'dart:async';

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  TextEditingController addressController = TextEditingController();
  String body = '';
  String barcodes = "";

  ScanResult scanResult;

  final _flashOnController = TextEditingController(text: "打开闪光灯");
  final _flashOffController = TextEditingController(text: "关闭闪光灯");
  final _cancelController = TextEditingController(text: "取消");

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(
            '二维码',
            style: TextStyle(
              color: Colors.white, //设置字体颜色
              fontSize: 20, //设置字体大小
            ),
          ),
        ),
        body: Container(
          padding: EdgeInsets.only(left: 15, right: 15, top: 25),
          constraints: BoxConstraints(
            minHeight: 60.0,
          ),
          child: Column(
            children: [
              TextField(
                  controller: addressController,
                  maxLines: 10,
                  decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    hintText: '请输入你要生成二维码的内容',
                    // prefixIcon: Icon(Icons.content_paste),
                  )),
              SizedBox(
                height: 50,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  RaisedButton(
                    child: new Text('生成二维码',
                        style: TextStyle(
                          color: Colors.white, //设置字体颜色
                          fontSize: 16, //设置字体大小
                        )),
                    color: Colors.orangeAccent,
                    onPressed: () {
                      body = addressController.text;
                      if (body == '') {
                        Toast.toast(context, msg: '请输入要生成的二维码内容');
                      } else {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) => QrcodePage(body: body),
                          ),
                        );
                        addressController.text = '';
                      }
                    },
                  ),
                  RaisedButton(
                    child: new Text('前去扫码',
                        style: TextStyle(
                          fontSize: 16, //设置字体大小
                        )),
                    onPressed: () {
                      // ...
                      scan();
                    },
                  )
                ],
              )
            ],
          ),
        ));
  }

  Future scan() async {
    try {
      var options = ScanOptions(strings: {
        "cancel": _cancelController.text,
        "flash_on": _flashOnController.text,
        "flash_off": _flashOffController.text,
      });

      var barcode = await BarcodeScanner.scan(options: options);
      if (barcode.rawContent != '') {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => ScancodePage(result: barcode.rawContent),
          ),
        );
      }
    } on PlatformException catch (e) {
      if (e.code == BarcodeScanner.cameraAccessDenied) {
        Toast.toast(context, msg: '未授予APP相机权限');
      } else {
        Toast.toast(context, msg: '扫码错误:: $e');
      }
    } on FormatException {
      Toast.toast(context, msg: '进入扫码页面后未扫码就返回');
    } catch (e) {
      Toast.toast(context, msg: '扫码错误: $e');
    }
  }
}
